using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace hosted_gmail_client
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
           ProcessLogin();
            
        }

       private void ProcessLogin() {
          using (RegistryKey key = Common.GetRegistryKey(true)) {
             key.SetValue("emailaddress",txtEmailAddress.Text);
             key.SetValue("password",txtPassword.Text);

             if (Common.AuthenticateUser(txtEmailAddress.Text,txtPassword.Text)) {
                this.DialogResult = DialogResult.OK;
                this.Hide();
             } else {
                lblErrorMessage.Text = "There was a problem logging you in.";
             }
          }
       }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            using (RegistryKey key = Common.GetRegistryKey(false))
            {
                txtEmailAddress.Text = key.GetValue("emailaddress").ToString();
                txtPassword.Text = key.GetValue("password").ToString();
            }
        }

       private void txtPassword_KeyPress(object sender,KeyPressEventArgs e) {
          if (e.KeyChar == 13) {
             ProcessLogin();  
          }
       }
    }
}